package com.lagou.config;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;
import java.util.Properties;

public class XMLMapperBuilder {
    private Configuration configuration;

    public XMLMapperBuilder(Configuration configuration) {
        this.configuration=configuration;
    }

    public void parse(InputStream in) throws DocumentException {
        Document document = new SAXReader().read(in);
        Element rootElement = document.getRootElement();
        String namespace = rootElement.attributeValue("namespace");
        List<Element> elements = rootElement.selectNodes("//select");
//        List<Element> updateelements = rootElement.selectNodes("//update");
//        List<Element> insertelements = rootElement.selectNodes("//insert");
//        List<Element> deleteelements = rootElement.selectNodes("//delete");
//        elements.addAll(updateelements);
//        elements.addAll(insertelements);
//        elements.addAll(deleteelements);
        for (Element sqlElement : elements) {
            String id = sqlElement.attributeValue("id");
            String resultType = sqlElement.attributeValue("resultType");
            String parameterType = sqlElement.attributeValue("parameterType");
            String sql = sqlElement.getTextTrim();
            MappedStatement statement = new MappedStatement();
            statement.setId(id);
            statement.setParameterType(parameterType);
            statement.setResultType(resultType);
            statement.setSql(sql);
            statement.setKey("select");
            String statementId = namespace+"."+id;
            configuration.getStatementMap().put(statementId,statement);
        }

        parseUpdate(namespace,rootElement);
        parseInsert(namespace,rootElement);
        parseDelete(namespace,rootElement);

    }

    private void parseDelete(String namespace, Element rootElement) {
        List<Element> elements = rootElement.selectNodes("//delete");
        for (Element sqlElement : elements) {
            String id = sqlElement.attributeValue("id");
            String parameterType = sqlElement.attributeValue("parameterType");
            String sql = sqlElement.getTextTrim();
            MappedStatement statement = new MappedStatement();
            statement.setId(id);
            statement.setParameterType(parameterType);
            statement.setResultType("int");
            statement.setSql(sql);
            statement.setKey("delete");
            String statementId = namespace+"."+id;
            configuration.getStatementMap().put(statementId,statement);
        }
    }

    private void parseInsert(String namespace, Element rootElement) {
        List<Element> elements = rootElement.selectNodes("//insert");
        for (Element sqlElement : elements) {
            String id = sqlElement.attributeValue("id");
            String parameterType = sqlElement.attributeValue("parameterType");
            String sql = sqlElement.getTextTrim();
            MappedStatement statement = new MappedStatement();
            statement.setId(id);
            statement.setParameterType(parameterType);
            statement.setResultType("int");
            statement.setSql(sql);
            statement.setKey("insert");
            String statementId = namespace + "." + id;
            configuration.getStatementMap().put(statementId, statement);
        }
    }

    private void parseUpdate(String namespace, Element rootElement) {
            List<Element> elements = rootElement.selectNodes("//update");
            for (Element sqlElement : elements) {
                String id = sqlElement.attributeValue("id");
                String parameterType = sqlElement.attributeValue("parameterType");
                String sql = sqlElement.getTextTrim();
                MappedStatement statement = new MappedStatement();
                statement.setId(id);
                statement.setParameterType(parameterType);
                statement.setResultType("int");
                statement.setSql(sql);
                statement.setKey("update");
                String statementId = namespace+"."+id;
                configuration.getStatementMap().put(statementId,statement);
        }
    }
}
